<?php
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
class PlotController extends \App\Http\Controllers\AdminController{
    /**
     * 小区列表
     * @return type
     */
    public function index() {
        $plots = \App\Models\Plot::whereIn('status', [1])->orderBy('id', 'asc')->paginate();
        $assign = compact('plots');
        return view('admin.plot.index', $assign);
    }
    
    /**
     * 修改/删除小区
     * @param Request $request
     * @param type $id
     * @param type $status
     * @return type
     */
    public function edit(Request $request, $id, $status=false) {
        $plot = \App\Models\Plot::find($id);
        if (!$plot) {
            return \Illuminate\Support\Facades\Response::json([
                'status'    => 0,
                'info'      => '小区不存在'
            ]);
        }
        if ($status === '0') {
            $plot->status = 0;
            $plot->save();
            return \Illuminate\Support\Facades\Response::json([
                'status'    => 1,
                'info'      => '删除成功'
            ]);
        }
        if ($request->ajax()) {
            $plot->name = $request->input('name');
            $plot->id = $request->input('id');
            $plot->area = $request->input('area');
            $plot->save();
            return \Illuminate\Support\Facades\Response::json([
                'status'    => 1,
                'info'      => '修改成功'
            ]);
        }
        $area = \App\Models\Area::orderBy('sort', 'asc')->get();
        $assign = compact('plot', 'area');
        return view('admin.plot.edit', $assign);
    }
    /**
     * 添加新小区
     */
    public function add(Request $request) {
        if ($request->ajax()) {
            return \App\Models\Plot::addPlot($request);
        }
        $area = \App\Models\Area::orderBy('sort', 'asc')->get();
        return view('admin.plot.add', ['area'=>$area]);
    }
}
